package com.gali.leetcode.day15;

import java.util.*;

/**
 * @Auther: gali
 * @Date: 2022-08-01 19:39
 * @Description:
 */
public class Solution {
    /**
     * 旋转图像
     * @param matrix
     */
    public void rotate(int[][] matrix) {
        int length = matrix.length;
        //水平翻转
        for (int i = 0; i < length/2; i++) {
            for (int j = 0; j < length; j++) {
                int temp=matrix[i][j];
                matrix[i][j]=matrix[length-i-1][j];
                matrix[length-i-1][j]=temp;
            }
        }
        //主对角线翻转
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < i; j++) {
                int temp=matrix[i][j];
                matrix[i][j]=matrix[j][i];
                matrix[j][i]=temp;
            }
        }
    }

    /**
     * 字母异位词分组
     * @param strs
     * @return
     */
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for(String str:strs){
            char[] chars = str.toCharArray();
            Arrays.sort(chars);
            String key = new String(chars);
            List<String> list = map.getOrDefault(key,new ArrayList<String>());
            list.add(str);
            map.put(key,list);
        }
        return new ArrayList<List<String>>(map.values());
    }
}
